﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Xml.Serialization;

namespace WebsiteLoadTool
{
    [Serializable]
    public class Website : INotifyPropertyChanged
    {
        private Guid id;
        private string name;
        private string uri;
        private string username;
        private string password;
        private string domain;

        [NonSerialized]
        private ObservableCollection<WebsiteLoadResult> results;

        public Website()
        {
            Results = new ObservableCollection<WebsiteLoadResult>();
        }


        public Guid ID
        {
            get { return id; }
            set
            {
                if (!Guid.Equals(id, value))
                {
                    id = value;
                    RaisePropertyChanged("ID");
                }
            }
        }

        public string Name
        {
            get { return name; }
            set
            {
                if (!string.Equals(name, value))
                {
                    name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        public string URI
        {
            get { return uri; }
            set
            {
                if (!string.Equals(uri, value))
                {
                    uri = value;
                    RaisePropertyChanged("URI");
                }
            }
        }

        public string Username
        {
            get { return username; }
            set
            {
                if (!string.Equals(username, value))
                {
                    username = value;
                    RaisePropertyChanged("Username");
                }
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if (!string.Equals(password, value))
                {
                    password = value;
                    RaisePropertyChanged("Password");
                }
            }
        }

        public string Domain
        {
            get { return domain; }
            set
            {
                if (!string.Equals(domain, value))
                {
                    domain = value;
                    RaisePropertyChanged("Domain");
                }
            }
        }


        [XmlIgnore]
        public ObservableCollection<WebsiteLoadResult> Results
        {
            get { return results; }
            set
            {
                if (!ObservableCollection<WebsiteLoadResult>.Equals(results, value))
                {
                    if (results != null)
                        Results.CollectionChanged -= ResultsCollectionChanged;

                    results = value;
                    RaisePropertyChanged("Results");

                    if (results != null)
                        Results.CollectionChanged += ResultsCollectionChanged;
                }
            }
        }

        [XmlIgnore]
        public WebsiteLoadResult LastResult
        {
            get
            {
                return (from r in Results
                        orderby r.StartDateTime descending
                        select r).FirstOrDefault();
            }
        }


        private void ResultsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged("LastResult");
        }

        #region INotifyPropertyChanged
        private event PropertyChangedEventHandler propertyChanged;

        public event PropertyChangedEventHandler PropertyChanged
        {
            add { propertyChanged += value; }
            remove { propertyChanged -= value; }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region public overrides
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is Website)) return false;
            if (Guid.Equals(this.ID, Guid.Empty)) return base.Equals(obj);
            return Guid.Equals(this.ID, ((Website)obj).ID);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            if (!string.IsNullOrEmpty(name)) return name;
            return base.ToString();
        }

        #endregion
    }
}
